var 
    express = require('express'),
    router = express.Router(),
    uuid = require('uuid'),
    _ = require('underscore'),
    UserModel = require('../db').get('User');




router.post('/user', function (req, res){
    var _d = req.body;
    

    var user = new UserModel({
        id: uuid.v1(),
        name: _d.name,
        email: _d.email,
        phone: _d.phone
        // groups: _d.groups.split(',')
    });

    user.save(function(e){
        if(!e){
            res.send({
                code:200,
                result:{
                    id: user.id
                }
            })
        }else{
            res.send({
                code: 500,
                msg: e.message
            });
        }
    });
            

});



router.get('/user', function(req, res) {
    var id = req.params.id;

    UserModel.
        find({}).
        limit(100).
        exec(function (error, docs){
            if(!error){
                res.send({
                    code:200,
                    result:{
                        users: docs
                    }
                })
            }else{
                res.send({
                    code: 500,
                    msg: error.message
                });
            }
        });
});


module.exports = router;
